//
//  ZMemberChoseVC.m
//  WeClubs
//
//  Created by Zed Link on 20/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZMemberChoseVC.h"
#import "ZCommentHeader.h"
#import "ZMemberManagerVC.h"
#import "ZMemberChoseVC.h"
#import "ZMemberModel.h"
#import "ZClubsListVC.h"
@interface ZMemberChoseVC ()
//数据源
@property(nonatomic)NSMutableArray *memberDataArr;
@end

@implementation ZMemberChoseVC

#pragma mark - Life Cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.mainView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    
    [self loadDataFormNetWork];

}


- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"选择成员";
}


- (void)loadDataFormNetWork{

    [MBProgressHUD zAlertProgress:@"正在加载中"];
    
    [HTTPRequest POST:kGetMyClubsURL parameter:@{@"sponsor_id":@([ZUserInfo sharedInstance].userId)} success:^(id resposeObject) {
        
        [MBProgressHUD zAlertHideHUD];
        
        if ([resposeObject[@"result_code"] integerValue] == ZNetManagerStatusSuccess) {
            
            for (NSDictionary *dic in resposeObject[@"data"][@"club"]) {
                
                ZMemberModel * model = [[ZMemberModel alloc]initWithDictionary:dic];
                
                [self.memberDataArr addObject:model];
            }
            [self.mainView reloadData];
            
        }else{
            [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
            
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD zAlertHideHUD];
    }];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return self.memberDataArr.count;
    }
        return 10;
}



#pragma mark - UITableViewDelegate & UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *sectionConfig = self.interfaceConfig[indexPath.section];
    
    switch (indexPath.section) {
        case 0:
        {
            ZNewClubCell *cell = [tableView dequeueReusableCellWithIdentifier:NewClubCellReuseID];
            cell.meberModel = self.memberDataArr[indexPath.row];
            return cell;
        }
            break;
        case 1:
        {
            ZNewMemberGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:NewMemberGroupCellReuseID];
            if (indexPath.row == sectionConfig.count - 1) {
                [cell hideSeparatorView];
            }
            return cell;
        }
            break;
        default:
        {
            return nil;
        }
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        // Todo: 删除已经保存的选择
        
        tableView.editing = NO;
    }];
    
    UITableViewRowAction *rename = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"重命名" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        // Todo: 重命名已经保存的选择
    }];
    rename.backgroundColor = HIGHTLIGHT_COLOR;
    
    UITableViewRowAction *save = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"保存" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        // Todo: 保存最近的选择
    }];
    save.backgroundColor = HIGHTLIGHT_COLOR;
    
    if (indexPath.section == 1) {
        // 保存的选择: 重命名, 删除
        return @[delete, rename];
    } else if (indexPath.section == 2) {
        // 最近的选择: 保存, 删除
        return @[delete, save];
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZMemberModel *model = self.memberDataArr[indexPath.row];
    switch (indexPath.section) {
        case 0:
        {
            
            ZClubsListVC *clubListVc = [ZClubsListVC zNew];
            clubListVc.club_id = model.club_id;
            clubListVc.title = model.club_name;
            [self.navigationController pushViewController:clubListVc animated:YES];
        }
            break;
        case 1:
        case 2:
        {
            // Todo: 已经选择指定成员, 返回上一级
            [self.navigationController popViewControllerAnimated:YES];
        }
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionConfig = self.interfaceConfig[section];
    NSString *headerTitle = sectionConfig[NewHeaderKey];
    
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TodoHeaderReuseID];
    header.style = ZTableHeaderStyleDetail;
    header.alignment = ZTableHeaderAlignmentTop;
    [header zSetTitle:headerTitle];
    
    return header;
}


- (NSMutableArray *)memberDataArr{

    if (!_memberDataArr) {
        _memberDataArr = [NSMutableArray array];
    }
    return _memberDataArr;
    

}

@end
